#pragma once

#include <QtWidgets/QMainWindow>
#include <QPair>
#include <QVector>
#include <QPainter>
#include <QMouseEvent>
#include "ui_contourLap.h"

 
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

class contourLap : public QMainWindow
{
    Q_OBJECT

public:
    contourLap(QWidget *parent = nullptr);
    ~contourLap();

private:
    Ui::contourLapClass ui;
    
    // 数据存储
    QVector<QPointF> wellTrajectoryPoints;
    QVector<QPolygonF> polygons;
    QVector<QColor> polygonColors;
    
    // 视图控制
    QPoint lastMousePos;
    bool isDragging;
    double scaleFactor;
    QPointF viewOffset;
    
    // 绘图相关方法
    void setupPlot();
    bool loadWellTrajectory();
    void loadPolygons();
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    
    // 坐标转换
    QPointF worldToScreen(const QPointF &worldPoint) const;
    QPointF screenToWorld(const QPointF &screenPoint) const;
    
    // 辅助函数
    double calculateWellTrajectoryAngle();
    QPointF calculateWellTrajectoryCenter();
    QPair<double, double> calculateWellTrajectoryExtents();
    QPair<double, double> calculateContourWidthExtents();
    
    // 绘制函数
    void drawWellTrajectory(QPainter &painter);
    void drawPolygons(QPainter &painter);
    void drawGrid(QPainter &painter);
    void drawCompass(QPainter &painter);
    void drawAxes(QPainter &painter);
    
    // 事件过滤器
    bool eventFilter(QObject *watched, QEvent *event) override;
};
